package com.rollcall.wisdomroll;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.rollcall.wisdomroll.utils.ToastUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class netsettings extends AppCompatActivity {
    private EditText fuwuip;
    private EditText mDuankou;
    private Button mOk;
    private String fuwIp;//定义的域名ip
    private String duankou;//定义密码
    /**
     * 正则表达式，匹配 URL 是IP还是域名
     */
    private static final String PATTERN_L2DOMAIN = "(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)";
    private static final String PATTERN_IP = "(\\d*\\.){3}\\d*";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_netsettings);
        init();
//        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
//        if(actionBar != null){
//            actionBar.setHomeButtonEnabled(true);
//            actionBar.setDisplayHomeAsUpEnabled(true);
//        }

    }
    public class TitleLayout extends LinearLayout {
        public TitleLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater.from(context).inflate(R.layout.header_layout, this);
        }

    }
    //@
    private void init() {
        mOk=findViewById(R.id.mOk);
        fuwuip=findViewById(R.id.fuwuip);
      //  mDuankou=findViewById(R.id.mDuankou);
        mOk.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                fuwIp = fuwuip.getText().toString();
              //  duankou = mDuankou.getText().toString();
                Log.e("域名ip",fuwIp);
                //Log.e("端口",duankou);
                if (TextUtils.isEmpty(fuwIp)) {//判断为空时
                    Toast.makeText(netsettings.this, "请输入服务器ip或域名", Toast.LENGTH_SHORT).show();//自带提示
                    return;
                }
//                else if (isURL2(fuwIp)) {//判断为空时
//                    Toast.makeText(netsettings.this, "请输入合法的服务器ip或域名", Toast.LENGTH_SHORT).show();//自带提示
//                    return;
//                }

//                else if (TextUtils.isEmpty(duankou)) {
//                    Toast.makeText(netsettings.this, "请输入端口号", Toast.LENGTH_SHORT).show();//自带提示
//                    return;
//                }
                else{
                    SharedPreferences preferences=getSharedPreferences("user",Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor=preferences.edit();
                    String fuwIpnum=fuwIp;
//                    String duankounum=duankou;
                    editor.putString("fuwIpnum", fuwIpnum);
                  //  editor.putString("duankounum", duankounum);
                    editor.commit();
                    finish();
                    ToastUtil.show(getApplicationContext(),"保存成功");
                }

            }
        });


    }
    public static boolean isURL2(String str){
        String regex = "^((https|http|ftp|rtsp|mms)?://)"
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@  
                + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184  
                + "|" // 允许IP和DOMAIN（域名） 
                + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.  
//                 + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名  
                + "[a-z]{2,6})" // first level domain- .com or .museum  
                + "(:[0-9]{1,4})?" // 端口- :80  
                + "((/?)|" // a slash isn't required if there is no file name  
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        return match( regex ,str );


    }

    /**
     * @param regex
     * 正则表达式字符串
     * @param str
     * 要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case android.R.id.left_back:
//                this.finish(); // back button
//                return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }

}
